package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.Payment;
import ua.kharkov.knure.mikhaylov.st4.db.entities.User;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Commands - redirects to payments viewing page previously extracting them
 * 
 * @author Saint
 * 
 */
public class ViewPaymentsCommand extends Command {

	private static final long serialVersionUID = 4386788892949388482L;
	private static final Logger log = Logger
			.getLogger(ViewPaymentsCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		log.debug("Command starts");

		HttpSession session = request.getSession();

		User user = (User) session.getAttribute("user");

		List<Payment> payments = DBManager.getInstance().findPaymentsByUser(
				user);
		session.setAttribute("payments", payments);

		log.debug("User payments extracted --> " + payments.size());

		log.debug("Command finished");
		return Path.PAGE_PAYMENTS;
	}
}
